<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class RegistrationApiController extends AbstractRestfulController
{
    public function getList()
    {			
			
    }
    public function get($user_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$userData = $userTable->getUserData($user_id)->toarray();
		if(count($userData)!=0){
			return new JsonModel(array(
				'userDetailes'  => json_encode($userData),
				'value' 		=> 1,
			));
		}else{
			return new JsonModel(array(
				'data' 	 => 'no data',
				'value'  => 0,
			));
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$userSettingTable=$this->getServiceLocator()->get('Models\Model\UserSettingsFactory');
		$check_User_Phonenumber = $userTable->check_User_Phonenumber($data);
		if($check_User_Phonenumber->count()){
			$updateUserData = $userTable->updateUserData($check_User_Phonenumber->buffer()->current()->user_id,$data);
			return new JsonModel(array(
				'output'	 		=> 'success',
				'user_id'	 		=>	$check_User_Phonenumber->buffer()->current()->user_id,
				'rating'	 		=>	$check_User_Phonenumber->buffer()->current()->rating,
				'radius'	 		=>	$check_User_Phonenumber->buffer()->current()->radius,
				'setting_rating'	=>	$check_User_Phonenumber->buffer()->current()->setting_rating,
				'pincode'	 		=>	$check_User_Phonenumber->buffer()->current()->pincode,
				'value' 	 		=> 1,
			));
		}else{
			$user_id = $userTable->saveUserData($data);
			$user_setting_id = $userSettingTable->saveUserData($user_id);
			if($user_id!=0){
				return new JsonModel(array(
					'output'	 => 'success',
					'user_id'	 =>	$user_setting_id,
					'rating'	 =>	0,
					'radius'	 =>	0,
					'pincode'	 =>	0,
					'value' 	 => 1,
				));
			}else{
				return new JsonModel(array(
					'output'	 => 'fail',
					'value' 	 => 0,
				));
			}
		}
    }
    public function update($user_id,$data)
    {
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$userSettingsTable=$this->getServiceLocator()->get('Models\Model\UserSettingsFactory');
		$updateUserFields = $userTable->updateUserFields($user_id,$data);
		$updateUserSettings=$userSettingsTable->updateUserSettings($user_id,$data);
		if($updateUserFields){
			return new JsonModel(array(
				'output'		 => 'success',
				'userdetailes'	 =>	$updateUserFields,
				'value' 	 	 => 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}